<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>新闻</button>
    <div class="new">

    </div>
</body>

</html>
<script>

    function ajax(method, url, renderNews) {
        //1、创建一个xhr对象
        let xhr = new XMLHttpRequest()

        //2、配置请求对象
        //参数一：method请求方式
        //参数二：url请求地址
        xhr.open(method, url)

        //3、设置回调函数
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                renderNews(JSON.parse(xhr.responseText))
            }
        }

        //4、发送请求
        xhr.send();
    }
    //给按钮添加点击事件
    document.querySelector('button').onclick = function () {
        ajax('get', './news.json', function (data) {
            var str = data.new.map(( item , index )=>{
                return `
                <h4> ${item.name} </h4>
                `
            }).join('')
            document.querySelector('.new').innerHTML = str
        })
    }
</script>